#include "PrecompiledHeader_Voxel.h"
#include "CGame.h"

/*--------------------------------------------------------------------------*/

CGame::CGame() :
	IGame("Voxel", ""),
	IRender(0)
{
	RenderTaskDesc desc;
	desc.m_material = NULL;
	desc.m_argument = 0;
	desc.m_flags = ERenderTaskFlag::UserInterface | ERenderTaskFlag::NoMaterial;
	desc.m_priority = 98;
	desc.m_object = this;

	this->AddRenderTask(desc);
}

/*--------------------------------------------------------------------------*/

CGame::~CGame()
{

}

/*--------------------------------------------------------------------------*/

Chunk* vao;

void CGame::InitGame()
{
	g_Sdl->SetWindowTitle(this->GetName());

	GLTextureDesc td;
	td.internalFormat = GL_RGBA8;
	td.height = 0;
	td.width = 0;
	td.data = NULL;
	td.format = GL_RGBA;
	td.type = GL_UNSIGNED_BYTE;
	td.name = "mc_test_output";
	this->m_outputTexture = ROpenGL::GenTexture(&td);

	CameraDesc cd;
	cd.m_farplane = 200.0f;
	cd.m_nearplane = 0.01f;
	cd.m_fov = 70.0f;
	cd.m_name = "ROOT_OF_ROOTS_CAMERA";
	cd.m_method = ERenderPhaseMethod::NormalRendering;//DeferredShading;
	cd.m_output = this->m_outputTexture;

	SceneInstance* sceneInstance = g_Scene->CreateScene();
	PerspectiveCamera* camera = sceneInstance->SetCamera(cd);
	sceneInstance->StartScene("mc", 0);

	NodeCreation nc;
	nc.m_flags = ENodeFlag::CubicShape;
	nc.m_parent = sceneInstance->GetRootNode();
	nc.m_scene = sceneInstance;


	m_world = new World(nc);
	m_world->Init();

	g_Scene->SetFreeCamera(camera);
}

/*--------------------------------------------------------------------------*/

void CGame::OnRender(const RenderTask* renderTask, uint camarg)
{
	m_world->Tick();

	ROpenGL::ClearBuffers();

	ROpenGL::BlendEnabled(GL_TRUE);
	ROpenGL::BlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	g_Renderer->DrawRectangle2D(glm::vec2(), g_Settings->GetResolutionf(), m_outputTexture);

	m_world->DrawDebugHud();
}

/*--------------------------------------------------------------------------*/

void CGame::OnLoopTick()
{
	m_world->Tick();

	this->BindRenderTasks(g_Renderer->GetUIRenderPhase());
}

/*--------------------------------------------------------------------------*/